/******************************************************************
 *
 *    Java Lib For Android, Powered By Shenzhen Jiuzhou.
 *
 *    Copyright (c) 2001-2014 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.etc.user.service.impl
 *
 *    Filename:    UserServiceImpl.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2014
 *
 *    Company:     Digital Telemedia Co.,Ltd
 *
 *    @author:     Administrator
 *
 *    @version:    1.0.0
 *
 *    Create at:   2016年3月22日 下午7:47:22
 *
 *    Revision:
 *
 *    2016年3月22日 下午7:47:22
 *        - first revision
 *
 *****************************************************************/
package com.etc.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.common.exception.DataOperationException;
import com.etc.common.exception.SysOperationException;
import com.etc.common.util.CustomBeanUtils;
import com.etc.user.dao.IUserMapper;
import com.etc.user.model.User;
import com.etc.user.service.IUserService;
import com.etc.user.vo.UserVO;
import com.github.pagehelper.PageHelper;


/**
 * @ClassName UserServiceImpl
 * @Description TODO(用户service实现)
 * @author 张亮
 * @Date 2016年3月22日 下午7:47:22
 * @version 1.0.0
 */
@Service("userService")
public class UserServiceImpl implements IUserService {
	
	Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private IUserMapper userMapper;
	
    @Override
    public Integer insert(UserVO vo) {
    	int res=0;
    	User user=new User();
    	try {
			CustomBeanUtils.copyProperties(user, vo);
			res=userMapper.insert(user);
		} catch (Exception e) {
			String errMsg = String.format("user保存异常,参数 :vo=%s", vo);
			log.error(errMsg, e);
			throw new DataOperationException(errMsg);
		} 
        return res;
    }

    @Override
    public void delete(Integer id) {
    	try {
			this.userMapper.delete(id);
		} catch (Exception e) {
			String errMsg = String.format("user删除异常,参数:id=%s", id);
			log.error(errMsg, e);
			throw new DataOperationException(errMsg);
		}
    }

    @Override
    public void update(UserVO vo) {
        User user=new User();
        try {
			CustomBeanUtils.copyProperties(user, vo);
			userMapper.update(user);
		} catch (Exception e) {
			String errMsg = String.format("user修改异常,参数:vo=%s", vo);
			log.error(errMsg, e);
			throw new DataOperationException(errMsg);
		} 
    }

    @Override
    public UserVO selectById(Integer id) {
    	UserVO userVO=new UserVO();
    	try {
    		User user=userMapper.selectById(id);
    		CustomBeanUtils.copyProperties(userVO, user);
		} catch (Exception e) {
			String errMsg = String.format("user查询异常,参数:id=%s", id);
			log.error(errMsg, e);
			throw new SysOperationException(errMsg);
		}
    	
        return userVO;
    }

    @Override
    public List<UserVO> list(Map<String, Object> param) {
    	try {
			PageHelper.startPage((Integer) param.get("pageNo"), (Integer) param.get("pageNum"));
			List<User> modelList = userMapper.list(param);
			List<UserVO> voList = new ArrayList<UserVO>();

			
			for (User model : modelList) {
				UserVO vo=new UserVO();
				CustomBeanUtils.copyProperties(vo, model);
				voList.add(vo);
			}
			return voList;
		} catch (Exception e) {
			String errMsg = String.format("user查询异常,参数 :param=%s", param);
			log.error(errMsg, e);
			throw new SysOperationException(errMsg);
		}
    }

    @Override
    public int count(Map<String, Object> param) {
        return userMapper.count(param);
    }

	@Override
	public UserVO findByName(String name) {
		try {
			UserVO userVO=new UserVO();
			Map<String, Object> param=new HashMap<String,Object>();
			param.put("tsuName", name);
			List<User> list=userMapper.list(param);
			if(null!=list){
				User user=list.get(0);
				CustomBeanUtils.copyProperties(userVO, user);
			}
			return userVO;
		} catch (Exception e) {
			String errMsg = String.format("user查询异常,参数 :param=%s", name);
			log.error(errMsg, e);
			throw new SysOperationException(errMsg);
		}
		
	}
    
}
